const {loadModule} = window['vue3-sfc-loader'];
import api from './api/index.js'
import config from "./config/index.js"
import http from "./utils/request.js"
import tool from './utils/tool.js'
import handler from './utils/handler.js'

export default {
    install(app) {
        // 挂载全局对象
        app.config.globalProperties.$API = api;
        app.config.globalProperties.$CONFIG = config;
        app.config.globalProperties.$HTTP = http;
        app.config.globalProperties.$TOOL = tool;

        // 注册全局组件
        app.component('jcxCodeEditor', Vue.defineAsyncComponent(() => loadModule("/src/components/jcxCodeEditor/index.vue", options)));
        app.component('jcxFormTable', Vue.defineAsyncComponent(() => loadModule("/src/components/jcxFormTable/index.vue", options)));
        app.component('jcxIconSelect', Vue.defineAsyncComponent(() => loadModule("/src/components/jcxIconSelect/index.vue", options)));
        app.component('jcxTableBar', Vue.defineAsyncComponent(() => loadModule("/src/components/jcxTableBar/index.vue", options)));
        app.component('jcxStatistic', Vue.defineAsyncComponent(() => loadModule("/src/components/jcxStatistic/index.vue", options)));
        app.component('jcxPage', Vue.defineAsyncComponent(() => loadModule("/src/components/jcxPage/index.vue", options)));

        // 注册全局指令

        // 统一注册 el-icon 图标
        for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
            app.component(`ElIcon${key}`, component)
        }

        // 统一注册 jcx-icon 图标
        app.component('JcxIconVue', Vue.defineAsyncComponent(() => loadModule('/src/assets/icons/Vue.vue', options)));
        app.component('JcxIconCode', Vue.defineAsyncComponent(() => loadModule('/src/assets/icons/Code.vue', options)));
        app.component('JcxIconWechat', Vue.defineAsyncComponent(() => loadModule('/src/assets/icons/Wechat.vue', options)));
        app.component('JcxIconBugFill', Vue.defineAsyncComponent(() => loadModule('/src/assets/icons/BugFill.vue', options)));
        app.component('JcxIconBugLine', Vue.defineAsyncComponent(() => loadModule('/src/assets/icons/BugLine.vue', options)));
        app.component('JcxIconFileWord', Vue.defineAsyncComponent(() => loadModule('/src/assets/icons/FileWord.vue', options)));
        app.component('JcxIconFileExcel', Vue.defineAsyncComponent(() => loadModule('/src/assets/icons/FileExcel.vue', options)));
        app.component('JcxIconFilePpt', Vue.defineAsyncComponent(() => loadModule('/src/assets/icons/FilePpt.vue', options)));
        app.component('JcxIconFileOrganization', Vue.defineAsyncComponent(() => loadModule('/src/assets/icons/Organization.vue', options)));
        app.component('JcxIconFileUpload', Vue.defineAsyncComponent(() => loadModule('/src/assets/icons/Upload.vue', options)));
        app.component('JcxIconFileDownload', Vue.defineAsyncComponent(() => loadModule('/src/assets/icons/Download.vue', options)));

        // 关闭 async-validator 全局控制台警告
        window.ASYNC_VALIDATOR_NO_WARNING = 1

        // 全局代码错误捕捉
        app.config.errorHandler = handler
    }
}
